package com.lx.huimail.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lx.huimail.MyApplication;
import com.lx.huimail.R;
import com.lx.huimail.configs.Config;
import com.lx.huimail.utils.DateTimePickDialogUtil;
import com.lx.huimail.utils.TestDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lx.huimail.weight.ViewHolder;
import okhttp3.Call;

public class MoneyWaterActivity extends Activity {
    private TextView shaixuan;
    private ListView lv;
    RelativeLayout fanhui;
    PopAdapter adapter;
    PoiAdapter madapter;
    private String custid;
    private String id;
    private String str;
    private String start,end,startTime,endTime;
public static Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_moneywater);
        activity=this;
        SharedPreferences sharedPreferences=getSharedPreferences("user",Context.MODE_PRIVATE);
        custid=sharedPreferences.getString("custid","-1111");
        shaixuan=(TextView)findViewById(R.id.shaixuan);
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MoneyWaterActivity.this.finish();
            }
        });
        lv=(ListView)findViewById(R.id.lv);
        SimpleDateFormat formatter = new SimpleDateFormat    ("yyyy年MM月dd日 HH:mm ");
        Date curDate =new Date(System.currentTimeMillis());//获取当前时间
        str = formatter.format(curDate);
        shaixuan.setOnClickListener(new View.OnClickListener() {
            private EditText dingdanhao;
            private TextView tv1;
            private TextView tv2;
            private TextView myTextView;
            private String initStartDateTime = str; // 初始化开始时间
            private String initEndDateTime = str; // 初始化结束时间
            private AlertDialog dialog;
            private Button ok;
            private  Button cancel;
            private Spinner mySpinner;
            private ArrayAdapter<String> adapter;
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.dialog_select1,
                        (ViewGroup)findViewById(R.id.dialog_select1));
                AlertDialog.Builder builder = new AlertDialog.Builder(MoneyWaterActivity.this);
                builder .setView(layout);
                dialog=builder.create();
                dialog.show();
                tv1=(TextView)layout.findViewById(R.id.tv1);
                tv2=(TextView) layout.findViewById(R.id.tv2);
                dingdanhao=(EditText)layout.findViewById(R.id.dingdanhao);
                ok=(Button)layout.findViewById(R.id.ok);
                cancel=(Button)layout.findViewById(R.id.cancel);
                tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
//                                MoneyWaterActivity.this, initEndDateTime);
//                        dateTimePicKDialog.dateTimePicKDialog(tv1);
                        TestDialog dialog  = new TestDialog(MoneyWaterActivity.this, R.style.Dialog);
                        dialog.setTitle("选择时间");
                        dialog.setBackButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setConfirmButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tv1.setText(TestDialog.getDate());
                            }
                        });
                        dialog.show();

                    }
                });
                tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil(
//                                MoneyWaterActivity.this, initEndDateTime);
//                        dateTimePicKDialog.dateTimePicKDialog(tv2);
                        TestDialog dialog  = new TestDialog(MoneyWaterActivity.this, R.style.Dialog);
                        dialog.setTitle("选择时间");
                        dialog.setBackButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        dialog.setConfirmButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                tv2.setText(TestDialog.getDate());
                            }
                        });

                        dialog.show();

                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap<String,String> map=new HashMap<String, String>();
                        map.put("custidEQ",custid);
                        map.put("cashnoEQ",dingdanhao.getText().toString());
                        map.put("typeEQ","6");
                        map.put("isbudgetEQ",id);
                        map.put("createtimeGE",tv1.getText().toString());
                        map.put("createtimeLE",tv2.getText().toString());
                        Gson gson=new Gson();
                        String str=gson.toJson(map).toString();
                        OkHttpUtils
                                .post()
                                .url(Config.url+"cash/pingtaisearchCash.do")
                                .addParams("data", str)
                                .addParams("mobile","true")
                                .addParams("page","1")
                                .addParams("rows","10000000")
                                .addParams("flag","1")
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Call call, Exception e, int id) {
                                        System.out.print("失败--"+e.getMessage().toString());
                                        Toast.makeText(MoneyWaterActivity.this,"服务器连接失败",Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onResponse(String response, int id) {
                                        System.out.print("提现筛选--"+response);
                                        List<Map<String, String>> Lists = new ArrayList<Map<String, String>>();
                                        try{
                                            JSONObject o1=new JSONObject(response);
                                            JSONArray a1=o1.getJSONArray("rows");
                                            if (a1.length()>0) {
                                                for (int i = 0; i < a1.length(); i++) {
                                                    JSONObject o3 = a1.getJSONObject(i);
                                                    Map<String, String> map = new HashMap<String, String>();
                                                    map.put("createtime", o3.getString("createtime"));
                                                    map.put("cashno", o3.getString("cashno"));
                                                    map.put("money", o3.getString("money"));
                                                    map.put("isbudget", o3.getString("isbudget"));
                                                    Lists.add(map);
                                                    PoiAdapter madapter = new PoiAdapter(MoneyWaterActivity.this, Lists);
                                                    lv.setAdapter(madapter);
                                                }
                                            }else{
                                                lv.setVisibility(View.GONE);
                                            }
                                        }catch(JSONException e){
                                            e.printStackTrace();
                                        }
                                    }

                                });
                        dialog.dismiss();
                    }
                });
                List<String> list = new ArrayList<String>();
                //添加一个下拉列表项的list，这里添加的项就是下拉列表的菜单项
                list.add("全部");
                list.add("未结算");
                list.add("已结算");
                mySpinner = (Spinner)layout.findViewById(R.id.Spinner_zhuangtai);
                //为下拉列表定义一个适配器，这里就用到里前面定义的list。
                adapter = new ArrayAdapter<String>(MoneyWaterActivity.this,android.R.layout.simple_spinner_item, list);
                //为适配器设置下拉列表下拉时的菜单样式。
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //将适配器添加到下拉列表上
                mySpinner.setAdapter(adapter);
                //为下拉列表设置各种事件的响应，这个事响应菜单被选中
                mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
                    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        if (arg2==0){
                            id="";
                        }
                        if (arg2==1){
                            id="0";
                        }
                        if (arg2==2){
                            id="1";
                        }
                 //=arg2+"";
                /* 将mySpinner 显示*/
                        arg0.setVisibility(View.VISIBLE);
                    }
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
                        myTextView.setText("NONE");
                        arg0.setVisibility(View.VISIBLE);
                    }
                });
        /*下拉菜单弹出的内容选项触屏事件处理*/
                mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
                    public boolean onTouch(View v, MotionEvent event) {
                        // TODO Auto-generated method stub
                        /**
                         *
                         */
                        return false;
                    }
                });
        /*下拉菜单弹出的内容选项焦点改变事件处理*/
                mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
                    public void onFocusChange(View v, boolean hasFocus) {
                        // TODO Auto-generated method stub

                    }
                });
            }
        });
        get();
    }
    private void get(){
        Map<String,String> map=new HashMap<String, String>();
        map.put("custidEQ",custid);
        map.put("typeEQ","6");
        map.put("isbudgetEQ","");
        Gson gson=new Gson();
        String str=gson.toJson(map).toString();
        OkHttpUtils
                .post()
                .url(Config.url+"cash/pingtaisearchCash.do")
                .addParams("data", str)
                .addParams("page", "1")
                .addParams("rows", "10000000")
                .addParams("flag", "1")
                .addParams("mobile","true")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(MoneyWaterActivity.this,"服务器连接失败",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        System.out.print("提现列表--"+response);
                        List<Map<String, String>> List = new ArrayList<Map<String, String>>();
                        try{
                            JSONObject o1=new JSONObject(response);
                            JSONArray a1=o1.getJSONArray("rows");
                            for(int i=0;i<a1.length();i++){
                                JSONObject o3=a1.getJSONObject(i);
                                Map<String,String> map=new HashMap<String,String>();
                                map.put("createtime",o3.getString("createtime"));
                                map.put("cashno", o3.getString("cashno"));
                                map.put("money", o3.getString("money"));
                                map.put("isbudget", o3.getString("isbudget"));
                                List.add(map);
                                PopAdapter adapter = new PopAdapter(MoneyWaterActivity.this,List);
                                lv.setAdapter(adapter);
                            }

                        }catch(JSONException e){
                            e.printStackTrace();
                        }
                    }

                });
    }

    public class PopAdapter extends BaseAdapter {
        private Context context;
        List<Map<String, String>> List;
        private LayoutInflater mInflater;
        public PopAdapter(Context context,List<Map<String, String>> List){
            this.List = List;
            this.context = context;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return List.size();
        }
        @Override
        public Object getItem(int position) {
            return List.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_tixian, null);
                holder.tv1 = (TextView)convertView.findViewById(R.id.tv1);
                holder.tv2 = (TextView)convertView.findViewById(R.id.tv2);
                holder.tv3 = (TextView)convertView.findViewById(R.id.tv3);
                holder.time = (TextView)convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            if (List.get(position).get("isbudget").equals("0")) {
                holder.tv1.setText("未结算");
            }else{
                holder.tv1.setText("已结算");
            }
            holder.tv2.setText("提现金额："+List.get(position).get("money")+"");
            holder.tv3.setText("单号："+List.get(position).get("cashno")+"");
            holder.time.setText(List.get(position).get("createtime"));

            return convertView;

        }
    }
    public  class ViewHolder{

        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView time;

    }
    public class PoiAdapter extends BaseAdapter {
        private Context context;
        List<Map<String, String>> data;
        private LayoutInflater mInflater;
        public PoiAdapter(Context context,List<Map<String, String>> data){
            this.data = data;
            this.context = context;
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return data.size();
        }
        @Override
        public Object getItem(int position) {
            return data.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_tixian, null);
                holder.tv1 = (TextView)convertView.findViewById(R.id.tv1);
                holder.tv2 = (TextView)convertView.findViewById(R.id.tv2);
                holder.tv3 = (TextView)convertView.findViewById(R.id.tv3);
                holder.time = (TextView)convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            if (data.get(position).get("isbudget").equals("0")) {
                holder.tv1.setText("未结算");
            }else{
                holder.tv1.setText("已结算");
            }
            holder.tv2.setText("提现金额："+data.get(position).get("money")+"");
            holder.tv3.setText("单号："+data.get(position).get("cashno")+"");
            holder.time.setText(data.get(position).get("createtime"));

            return convertView;

        }
    }

    /** @SuppressWarnings("unused")
    private String getData(String timeString){
    Long time = Long.parseLong(timeString)*1000;
    String date = new java.text.SimpleDateFormat("MM月dd日 HH:mm:ss").format(new java.util.Date(time));
    return date;
    }*/
}
